package com.shiguiwu.springmybatis.spring.lifecycle.definition;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

/**
 * @description: 1，bean定义信息
 * @author: stone
 * @date: Created by 2021/3/17 14:08
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.lifecycle.definition
 */
public class DefinitionTests {

    public static void main(String[] args) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(Pig.class.getName());
        builder.addPropertyValue("name", "小猪佩奇");
        builder.addPropertyValue("age", 18);
        BeanDefinition definition = builder.getBeanDefinition();
        System.out.println(definition);

        //依赖关系的
        BeanDefinition definition1 = BeanDefinitionBuilder.rootBeanDefinition(User.class)
                .addPropertyValue("name", "小明")
                .addPropertyReference("pig", "pig")
                .getBeanDefinition();


        //创建一个容器
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        //注册bean
        beanFactory.registerBeanDefinition("pig", definition);
        beanFactory.registerBeanDefinition("user", definition1);
        Object pig = beanFactory.getBean("pig");
        Object user = beanFactory.getBean("user");
        System.out.println(pig);
        System.out.println(user);
    }
}
